package com.cy.mall.service.customer.impl;

import com.cy.mall.comm.utils.CommonUtils;
import com.cy.mall.dao.mapper.customer.CustomerMapper;
import com.cy.mall.dao.mapper.customer.CustomerWechatMapper;
import com.cy.mall.model.domain.customer.Customer;
import com.cy.mall.model.domain.customer.CustomerWechat;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.customer.CustomerWechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description 客户-微信 服务
 * @Author tanyilin
 * @Date 2019/3/15 11:24
 */
@Service
public class CustomerWechatServiceImpl extends BaseService implements CustomerWechatService {

    @Autowired
    private CustomerWechatMapper customerWechatMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public CustomerWechat selectByCustomerId(Long customerId) {

        CustomerWechat customerWechat = new CustomerWechat();
        customerWechat.setCustomerId(customerId);
        customerWechat = customerWechatMapper.selectOne(customerWechat);
        return customerWechat;
    }

    @Override
    public CustomerWechat selectByOpenId(String openId) {

        CustomerWechat customerWechat = new CustomerWechat();
        customerWechat.setOpenId(openId);
        customerWechat = customerWechatMapper.selectOne(customerWechat);
        return customerWechat;
    }

    @Override
    @Transactional
    public void insertCustomerWechat(CustomerWechat customerWechat) {
        Date time = new Date();
        //先删除
        //删除openId旧绑定关系
        CustomerWechat delOpenId = new CustomerWechat();
        delOpenId.setOpenId(customerWechat.getOpenId());
        customerWechatMapper.delete(delOpenId);

        //删除customerId旧绑定关系
        CustomerWechat delCustomerId = new CustomerWechat();
        delCustomerId.setCustomerId(customerWechat.getCustomerId());
        customerWechatMapper.delete(delCustomerId);

        //再新增
        customerWechat.setCreateTime(time);
        customerWechat.setUpdateTime(time);
        customerWechatMapper.insertSelective(customerWechat);

        //更新 customer 里的昵称
        if(!CommonUtils.isEmpty(customerWechat.getNickName())) {
            Customer updateCustomer = new Customer();
            updateCustomer.setId(customerWechat.getCustomerId());
            updateCustomer.setNickName(customerWechat.getNickName());
            updateCustomer.setUpdateTime(time);
            customerMapper.updateByPrimaryKeySelective(updateCustomer);
        }

    }

    @Override
    @Transactional
    public void updateCustomerWechat(CustomerWechat customerWechat) {
        Date time = new Date();
        //更新客户-微信关联关系 CustomerWechat
        customerWechat.setUpdateTime(time);
        customerWechatMapper.updateByPrimaryKeySelective(customerWechat);
        //更新 customer 里的昵称
        if(!CommonUtils.isEmpty(customerWechat.getNickName())) {
            Customer updateCustomer = new Customer();
            updateCustomer.setId(customerWechat.getCustomerId());
            updateCustomer.setNickName(customerWechat.getNickName());
            updateCustomer.setUpdateTime(time);
            customerMapper.updateByPrimaryKeySelective(updateCustomer);
        }
    }

    @Override
    @Transactional
    public void deleteCustomerWechat(CustomerWechat customerWechat) {
        customerWechatMapper.delete(customerWechat);
    }


}
